<!--
 * @Author: 许浩锋
 * @Date: 2021-06-30 20:53:20
 * @LastEditors: 许浩锋
 * @LastEditTime: 2021-07-05 13:51:24
 * @Description:
-->
<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card body-style="{height: 100%}">
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input
            clearable
            @clear="getOrderList"
            placeholder="请输入内容"
            v-model="querInfo.query"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getOrderList"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-table :data="OrderList" border :stripe="true">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="order_number" label="订单编号" width="">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格" width="">
        </el-table-column>
        <el-table-column prop="pay_status" label="是否付款" width="">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.pay_status === '0'"
              >未付款</el-tag
            >
            <el-tag type="success" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" width="">
        </el-table-column>
        <el-table-column prop="create_time" label="下单时间" width="">
          <template slot-scope="scope">
            {{ scope.row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column prop="order_id" label="操作" width="">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="editAddress(scope.row.order_id)"
            >
            </el-button>
            <el-button
              @click="showProgressBox"
              size="mini"
              type="success"
              icon="el-icon-location"
            >
            </el-button>
            <el-button
              @click="showOrder(scope.row.order_id)"
              size="mini"
              type="info"
              icon="el-icon-s-order"
            >
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="querInfo.pagenum"
        :page-sizes="[5, 10, 20, 40]"
        :page-size="querInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <el-dialog
        @close="addressDialogClosed"
        title="修改地址"
        :visible.sync="editdialogVisible"
        width="50%"
      >
        <el-form
          :model="addressForm"
          :rules="addressFormRules"
          ref="addressFormRef"
          label-width="100px"
        >
          <el-form-item label="省市区/县" prop="address1">
            <el-cascader
              v-model="addressForm.address1"
              :options="cityData"
              :props="{ expandTrigger: 'hover' }"
            ></el-cascader>
          </el-form-item>
          <el-form-item label="详细地址" prop="address2">
            <el-input v-model="addressForm.address2"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editdialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editdialogVisible = false"
            >确 定</el-button
          >
        </span>
      </el-dialog>
      <!-- 物流进度 -->
      <el-dialog title="物流进度" :visible.sync="progressVisible" width="50%">
        <!-- 时间线 -->
        <el-timeline>
          <el-timeline-item
            v-for="(activity, index) in progressInfo"
            :key="index"
            :timestamp="activity.time"
          >
            {{ activity.context }}
          </el-timeline-item>
        </el-timeline>
      </el-dialog>
      <el-dialog title="订单详情" :visible.sync="orderVisible" width="50%">
        <el-form :model="orderForm" label-width="100px">
          {{ orderForm }}
        </el-form>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import cityData from './citydata'
export default {
  data() {
    return {
      OrderList: [],
      querInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10,
        user_id: '',
        pay_status: '',
        is_send: '',
        order_fapiao_title: '',
        order_fapiao_company: '',
        order_fapiao_content: '',
        consignee_addr: '',
      },
      total: 0,
      editdialogVisible: false,
      addressForm: {
        address1: [],
        address2: '',
      },
      addressFormRules: {
        address1: [
          { required: true, message: '请选择省市区县', trigger: 'blur' },
        ],
        address2: [
          { required: true, message: '请选择省市区县', trigger: 'blur' },
        ],
      },
      cityData,
      progressVisible: false,
      progressInfo: [],
      orderVisible: false,
      orderForm: [],
    }
  },
  methods: {
    async getOrderList() {
      const { data: res } = await this.$http.get(`orders`, {
        params: this.querInfo,
      })
      if (res.meta.status != 200) return this.$message.error('获取订单列表失败')
      this.$message.success('获取订单列表成功')
      this.OrderList = res.data.goods
      this.total = res.data.total
      console.log(res)
    },
    handleSizeChange(size) {
      this.querInfo.pagesize = size
      this.getOrderList()
    },
    handleCurrentChange(page) {
      this.querInfo.pagenum = page
      this.getOrderList()
    },
    editAddress() {
      this.editdialogVisible = true
    },
    addressDialogClosed() {
      this.$refs.addressFormRef.resetFields()
    },
    async showProgressBox() {
      const { data: res } = await this.$http.get('/kuaidi/804909574412544580')
      if (res.meta.status !== 200) {
        return this.$message.error('获取物流进度失败！')
      }

      this.progressInfo = res.data

      this.progressVisible = true
      console.log(this.progressInfo)
    },
    async showOrder(id) {
      const { data: res } = await this.$http.get(`orders/${id}`)
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单详情失败！')
      }
      this.orderForm = res.data
      this.orderVisible = true
    },
  },
  created() {
    this.getOrderList()
  },
}
</script>
<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
